package com.project.springboot.controller;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import javax.annotation.Resource;

/**
 * Description：测试Controller
 *              使用@WebMvcTest注解，需要指定测试哪些controllers，不需要运行在web环境下
 * Created by 朱正磊 on 2019/3/11 0011 下午 3:26
 */
@RunWith(SpringRunner.class)
@WebMvcTest(controllers = BookController.class)
public class BookControllerTest2 {

    @Resource
    private MockMvc mvc;

    @Test
    public void home() throws Exception {
        mvc.perform(MockMvcRequestBuilders.get("/book/home"))
                .andExpect(MockMvcResultMatchers.status().isOk());
        mvc.perform(MockMvcRequestBuilders.get("/book/home"))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.content().string("bookhome"));
    }

    @Test
    public void show() throws Exception {
        mvc.perform(MockMvcRequestBuilders.get("/book/show").param("id", "10"))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.content().string("book10"));
    }

}